<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="线路管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="线路组" prop="lineGroupId">
                <el-select
                    v-model="formData.lineGroupId"
                    filterable
                    placeholder="请选择"
                >
                    <el-option
                        v-for="item in lineGroupList"
                        :key="item.id"
                        :label="item.lineGroupName"
                        :value="item.id"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="线路名称" prop="lineName">
                <el-input v-model="formData.lineName"/>
            </el-form-item>
            <el-form-item label="简拼" prop="lineSpell">
                <el-input v-model="formData.lineSpell"/>
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark"/>
            </el-form-item>

            <el-form-item label="排序" prop="sort">
                <el-input-number v-model="formData.sort"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <template #reference>
                        <el-button slot="reference" type="primary">保存</el-button>
                    </template>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {LineGroupApi} from '@/api/intercity/LineGroupApi'
import {LineApi} from '@/api/intercity/LineApi'
import {Common} from '@/utils/common'

export default {
    name: 'LineUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                lineGroupId: '',
                lineName: '',
                lineSpell: '',
                remark: '',
                sort: 0
            },
            rules: {
                lineGroupId: [
                    {required: true, message: '请输入选择总线路组', trigger: 'change'}
                ],
                lineName: [
                    {required: true, message: '请输入线路名称', trigger: 'blur'},
                    {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            lineGroupList: []
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            this.getLineGroupList()
            if (data.id > 0) {
                this.$nextTick(() => {
                    this.formData = Common.assign(this.formData, data)
                })
            } else {
                this.formData.id = 0
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }
            }
        },

        // 获取总线路组列表
        getLineGroupList() {
            LineGroupApi.getList().then((data) => {
                this.lineGroupList = data
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        LineApi.update(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        LineApi.insert(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>
